package com.glorysoft.sunnypda.ui.downloadrecipe

import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.entity.EqpByAreaInfoEntity
import com.glorysoft.sunnypda.entity.EqpDataInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: DownLoadRecipeViewModel
 * Author: XJH
 * Date: 2022/1/11 16:56
 * Description:
 * History:
 */
class DownLoadRecipeViewModel : BaseViewModel() {

    val eqpRepository : EqpRepository by lazy { EqpRepository() }
    var mRecipeList : ArrayList<String> = arrayListOf()
    var mRecipeData : ArrayList<EqpByAreaInfoEntity> = arrayListOf()
    var eqpID = ""
    var type = ""

    var eqpItems = ObservableArrayList<EqpByAreaInfoEntity>()
    var eqpDataItems = ObservableArrayList<EqpDataInfoEntity>()
    val onItemClick : ItemClick = object : ItemClick {
        override fun onItemClick(item: EqpByAreaInfoEntity) {
            if (item.STATUS == "失效" || item.STATUS == "0"){
                defUI.callObserve.value = CallObserveBean(1)
            }else{
                getRecipeDownLoad(item)
            }
        }
    }
    val onDeleteClick : DeleteEqp = object : DeleteEqp {
        override fun onDelete(item: EqpDataInfoEntity) {
            eqpDataItems.remove(item)
        }
    }

    var eqpBinding =
        ItemBinding.of<EqpByAreaInfoEntity>(BR.eqpItem, R.layout.item_recipe_info)
            .bindExtra(BR.onItemClick,onItemClick)
    var eqpDataBinding =
        ItemBinding.of<EqpDataInfoEntity>(BR.eqpDataItem, R.layout.item_eqp_data)
            .bindExtra(BR.onDeleteClick,onDeleteClick)

    fun getRecipeList(eqp : String){
        eqpID = eqp
        val map = JsonObject()
        map.addProperty("EQPID",eqp)
        launchOnlyResult({
            mRecipeList.clear()
            mRecipeData.clear()
            eqpDataItems.clear()
            eqpItems.clear()
            val recipeList = eqpRepository.getRecipeList(map)
            mRecipeData = recipeList.RECIPELIST as ArrayList<EqpByAreaInfoEntity>
            eqpDataItems.addAll(recipeList.EQPS)
            recipeList.RECIPELIST.forEach{
                mRecipeList.add(it.RECIPENAME!!)
            }
            eqpDataItems.forEach {
                it.isGone = it.EQPID == eqp
            }
        })

    }

    fun getRecipeInfoByRecipe(codeS: String) {
        type = codeS
        launchOnlyResult({
            eqpItems.clear()
            mRecipeData.forEach {
                if (it.RECIPENAME == codeS){
                    when(it.STATUS){
                        "-1" ->{
                            it.MSTATUS = "临时"
                        }
                        "0" ->{
                            it.MSTATUS = "失效"
                        }
                        "1" ->{
                            it.MSTATUS = "待激活"
                        }
                        "2" ->{
                            it.MSTATUS = "有效"
                        }
                    }
                    when(it.VERSIONTYPE){
                        "0" ->{
                            it.MVERSIONTYPE = "EQP"
                        }
                        "1" ->{
                            it.MVERSIONTYPE = "GOLD"
                        }
                    }
                    if (eqpDataItems.size == 0){
                        eqpItems.add(it)
                    }else{
                        if (it.MVERSIONTYPE == "GOLD"){
                            eqpItems.add(it)
                        }
                    }

                }
            }
        })
    }

    fun getRecipeDownLoad(eqpData: EqpByAreaInfoEntity) {
        val map = JsonObject()
        map.addProperty("EQUIPMENTID",eqpData.EQUIPMENTID)
        map.addProperty("RECIPENAME",eqpData.RECIPENAME)
        map.addProperty("VERSION",eqpData.VERSION)
        map.addProperty("VERSIONTYPE",eqpData.VERSIONTYPE)
        map.addProperty("STATUS",eqpData.STATUS)
        map.addProperty("EQPID",eqpID)
        map.add("EQPS", JsonParser().parse(Gson().toJson(eqpDataItems)).asJsonArray)
        launchOnlyResult({
            eqpRepository.getRecipeDownLoad(map)
            defUI.callObserve.value = CallObserveBean(0)
        })
    }
    fun onDeleteList() {
        eqpDataItems.clear()
        getRecipeInfoByRecipe(type)
    }

    interface ItemClick{
        fun onItemClick(item : EqpByAreaInfoEntity)
    }
    interface DeleteEqp{
        fun onDelete(item : EqpDataInfoEntity)
    }
}